/*
 * @Date: 2023-08-14 19:23:04
 * @LastEditors: lyx
 * @LastEditTime: 2023-11-16 09:37:17
 * @FilePath: \sup-mimi-program-server\src\weapp\weapp.controller.ts
 * @Description:
 */
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
} from '@nestjs/common';
import { WeappService } from './weapp.service';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { SendMessageDto } from './dto/send-message';
import { CreateWeappDto } from './dto/create-weapp.dto';
import { UpdateWeappDto } from './dto/update-weapp.dto';
import { Inject } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { IGetOpenIdFromWx } from './dto/query-openid.dto';

@Controller('weapp')
@ApiTags('微信小程序服务端接口')
export class WeappController {
  @Inject(ConfigService)
  private configService: ConfigService;

  constructor(private readonly weappService: WeappService) {}

  @Post('/openId/save')
  @ApiOperation({ summary: '保存用户openId' })
  create(@Body() createDto: CreateWeappDto) {
    return this.weappService.createWxInfo(createDto);
  }

  @Get('/openId/getList')
  @ApiOperation({ summary: '获取用户openId列表' })
  findAll() {
    return this.weappService.findAll();
  }

  @Patch('/openId/:id')
  @ApiOperation({ summary: '更新用户openId' })
  update(@Param('id') id: string, @Body() updateWeappDto: UpdateWeappDto) {
    return this.weappService.updateWxInfo(id, updateWeappDto);
  }

  @Delete('/openId/:id')
  @ApiOperation({ summary: '删除openId' })
  remove(@Param('id') id: string) {
    return this.weappService.removeWxInfo(id);
  }

  @Get('/openId/:phoneNumber')
  @ApiOperation({ summary: '根据手机号查询openId' })
  findOneByPhoneNumber(@Param('phoneNumber') phoneNumber: string) {
    return this.weappService.getOpenIdFromDb(phoneNumber);
  }

  @Post('/openId/getOpenIdFromWx/:code')
  @ApiOperation({
    summary:
      '获取 openId [https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html]',
  })
  getOpenIdByWx(@Param('code') code: string, @Body() body: IGetOpenIdFromWx) {
    return this.weappService.getOpenIdByWx(code, body);
  }

  @Get('/getAccessToken')
  @ApiOperation({
    summary:
      '获取 Access token [https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html]',
  })
  getAccessToken() {
    return this.weappService.getAccessToken();
  }
  @Get('/getPhoneNumber/:code')
  @ApiOperation({
    summary:
      '获取 手机号(需要企业认证) [https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-info/phone-number/getPhoneNumber.html]',
  })
  getPhoneNumber(@Param('code') code: string) {
    return this.weappService.getPhoneNumber(code);
  }

  @Post('/sendMessage')
  @ApiOperation({
    summary:
      '发送订阅消息 [https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-message-management/subscribe-message/sendMessage.html]',
  })
  async sendMessage() {
    return this.weappService.sendMessage();
  }
}
